﻿using Kingdee.BOS;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace RK.Tools
{
    public class SyncLog
    {

        /// <summary>
        /// 主键ID
        /// </summary>
        private long fID;

        /// <summary>
        /// 组织Id
        /// </summary>
        private long fOrgId;

        /// <summary>
        /// 操作名称
        /// </summary>
        private string operation;
        /// <summary>
        /// 单据标识
        /// </summary>
        private string formId;
        /// <summary>
        /// 单据编号
        /// </summary>
        private string fBillNo;
        /// <summary>
        /// 单据名称
        /// </summary>
        private string fbillName;
        /// <summary>
        /// 同步时间
        /// </summary>
        private DateTime fmodifyDate;
        /// <summary>
        /// 同步结果
        /// </summary>
        private string result;

        /// <summary>
        /// 同步JSON
        /// </summary>
        private string json;

        /// <summary>
        /// 同步方向
        /// </summary>
        private string direction;

        /// <summary>
        /// 同步成功
        /// </summary>
        private bool isSuccess;


        public SyncLog()
        {
            this.fOrgId = 0;
            this.operation = "";
            this.formId = "";
            this.fBillNo = "";
            this.fbillName = "";
            this.fmodifyDate = DateTime.Now;
            this.result = "";
            this.json = "";
            this.direction = "";
            this.IsSuccess = true;
        }

        public SyncLog(long fOrgId, string operation, string formId, string fBillNo, string fbillName, DateTime fmodifyDate, string result, string json, string direction)
        {
            this.fOrgId = fOrgId;
            this.operation = operation;
            this.formId = formId;
            this.fBillNo = fBillNo;
            this.fbillName = fbillName;
            this.fmodifyDate = fmodifyDate;
            this.result = result;
            this.json = json;
            this.direction = direction;
        }

        public long FID { get => fID; set => fID = value; }
        public string Operation { get => operation; set => operation = value; }
        public string FormId { get => formId; set => formId = value; }
        public string FBillNo { get => fBillNo; set => fBillNo = value; }
        public string FbillName { get => fbillName; set => fbillName = value; }
        public DateTime FmodifyDate { get => fmodifyDate; set => fmodifyDate = value; }
        public string Result { get => result; set => result = value; }
        public string Json { get => json; set => json = value; }
        public string Direction { get => direction; set => direction = value; }
        public long FOrgId { get => fOrgId; set => fOrgId = value; }
        public bool IsSuccess { get => isSuccess; set => isSuccess = value; }

        public SqlParam[] CreateParam()
        {

            SqlParam[] sqlParams = {
                new SqlParam("@Operation",KDDbType.String,this.Operation),
                new SqlParam("@Json",KDDbType.String,this.Json),
                new SqlParam("@Result",KDDbType.String,this.Result),
                new SqlParam("@FBillNo",KDDbType.String,this.FBillNo),
                new SqlParam("@FormId",KDDbType.String,this.FormId),
                new SqlParam("@FbillName",KDDbType.String,this.FbillName),
                new SqlParam("@FOrgId",KDDbType.String,this.FOrgId),
                new SqlParam("@IsSuccess",KDDbType.String,this.IsSuccess)
            };
            return sqlParams;
        }

        public void GetInsertSqlAndParams(out List<SqlParam> sqlParams, out string sql)
        {
            FieldInfo[] fieldInfos = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            sqlParams = new List<SqlParam>();
            List<string> columns = new List<string>();
            List<string> columnParams = new List<string>();
            foreach (var fieldInfo in fieldInfos)
            {
                if (fieldInfo.Name.EqualsIgnoreCase("fID"))
                {
                    continue;
                }
                SqlParam param = new SqlParam("@" + fieldInfo.Name, KDDbType.String, fieldInfo.GetValue(this));
                sqlParams.Add(param);
                columns.Add(fieldInfo.Name);
                columnParams.Add("@" + fieldInfo.Name);
            }
            sql = string.Format("/*dialect*/ insert into T_Log_SyncLogs({0}) values({1})", string.Join(",", columns), string.Join(",", columnParams));
        }
    }
}
